import dotenv from 'dotenv';
import path from 'path';

// 加载环境变量
dotenv.config();

/**
 * 应用配置接口
 */
export interface AppConfig {
  env: string;
  port: number;
  name: string;
  version: string;
}

/**
 * 数据库配置接口
 */
export interface DatabaseConfig {
  mysql: {
    host: string;
    port: number;
    username: string;
    password: string;
    database: string;
  };
  mongodb: {
    uri: string;
    testUri: string;
  };
}

/**
 * JWT 配置接口
 */
export interface JwtConfig {
  secret: string;
  expiresIn: string;
}

/**
 * 日志配置接口
 */
export interface LogConfig {
  level: string;
  filePath: string;
}

/**
 * Redis 配置接口
 */
export interface RedisConfig {
  host: string;
  port: number;
  password?: string;
}

/**
 * 限流配置接口
 */
export interface RateLimitConfig {
  windowMs: number;
  maxRequests: number;
}

/**
 * CORS 配置接口
 */
export interface CorsConfig {
  origin: string;
  credentials: boolean;
}

/**
 * Swagger 配置接口
 */
export interface SwaggerConfig {
  enabled: boolean;
  path: string;
}

/**
 * 完整配置接口
 */
export interface Config {
  app: AppConfig;
  database: DatabaseConfig;
  jwt: JwtConfig;
  bcrypt: {
    saltRounds: number;
  };
  log: LogConfig;
  redis: RedisConfig;
  rateLimit: RateLimitConfig;
  cors: CorsConfig;
  swagger: SwaggerConfig;
}

/**
 * 获取环境变量值，如果不存在则使用默认值
 */
const getEnv = (key: string, defaultValue: string = ''): string => {
  return process.env[key] || defaultValue;
};

/**
 * 获取数字类型的环境变量
 */
const getEnvNumber = (key: string, defaultValue: number): number => {
  const value = process.env[key];
  return value ? parseInt(value, 10) : defaultValue;
};

/**
 * 获取布尔类型的环境变量
 */
const getEnvBoolean = (key: string, defaultValue: boolean): boolean => {
  const value = process.env[key];
  return value ? value.toLowerCase() === 'true' : defaultValue;
};

/**
 * 应用配置
 */
export const config: Config = {
  app: {
    env: getEnv('NODE_ENV', 'development'),
    port: getEnvNumber('PORT', 3000),
    name: getEnv('APP_NAME', 'jmtemp-server'),
    version: getEnv('APP_VERSION', '1.0.0'),
  },
  database: {
    mysql: {
      host: getEnv('MYSQL_HOST', 'localhost'),
      port: getEnvNumber('MYSQL_PORT', 3306),
      username: getEnv('MYSQL_USERNAME', 'root'),
      password: getEnv('MYSQL_PASSWORD', 'password'),
      database: getEnv('MYSQL_DATABASE', 'jmtemp_db'),
    },
    mongodb: {
      uri: getEnv('MONGODB_URI', 'mongodb://localhost:27017/jmtemp_db'),
      testUri: getEnv('MONGODB_TEST_URI', 'mongodb://localhost:27017/jmtemp_test_db'),
    },
  },
  jwt: {
    secret: getEnv('JWT_SECRET', 'your-super-secret-jwt-key'),
    expiresIn: getEnv('JWT_EXPIRES_IN', '7d'),
  },
  bcrypt: {
    saltRounds: getEnvNumber('BCRYPT_SALT_ROUNDS', 10),
  },
  log: {
    level: getEnv('LOG_LEVEL', 'info'),
    filePath: getEnv('LOG_FILE_PATH', path.join(process.cwd(), 'logs/app.log')),
  },
  redis: {
    host: getEnv('REDIS_HOST', 'localhost'),
    port: getEnvNumber('REDIS_PORT', 6379),
    password: getEnv('REDIS_PASSWORD'),
  },
  rateLimit: {
    windowMs: getEnvNumber('RATE_LIMIT_WINDOW_MS', 900000), // 15 minutes
    maxRequests: getEnvNumber('RATE_LIMIT_MAX_REQUESTS', 100),
  },
  cors: {
    origin: getEnv('CORS_ORIGIN', 'http://localhost:3000'),
    credentials: getEnvBoolean('CORS_CREDENTIALS', true),
  },
  swagger: {
    enabled: getEnvBoolean('SWAGGER_ENABLED', true),
    path: getEnv('SWAGGER_PATH', '/api-docs'),
  },
};
console.log('config:', config);

export default config;